﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeGraphicalControl;
using FeatureModelCore;
using FeatureModelTool.FeatureModelView.TreeViewItems;
using Collections.TreeCollections;
using System.Windows.Media;
using Collections;
using System.Windows;
using System.Windows.Controls;
using FeatureModel.Collections.TreeArgs;
using FeatureModelCore.FMCrossTreeConstraint;
using EventLogAssembly;

namespace FeatureModelTool.FeatureModelView
{
    public class FeatureTreeView : TreeViewControl
    {
        public static readonly Brush COMMON_BACKGROUND = Brushes.LightSteelBlue;
        public static readonly Brush ERROR_BACKGROUND = Brushes.LightCoral;
        public static readonly Brush WARNING_BACKGROUND = Brushes.LightSalmon;

        public FeatureTreeView()
            : base()
        {
            //Background = Brushes.White;
        }

        public override void Refresh(object sender, TreeEventArgs args)
        {
            base.Refresh(sender, args);
            // add cross tree constraint
            if(_versionController == null)
            {
                return;
            }
            FeatureModelTree tree = this._versionController.CurrentTree as FeatureModelTree;
            if (tree == null)
            {
                return;
            }

            foreach (CrossTreeConstraint cons in tree.AllCrossTreeConstraints)
            {
                TreeItem_Constraint treeItem_cons = TreeItem_Constraint.ConstructTreeItem(cons);
                treeItem_cons.ContextMenu = TreeItem_Constraint.GenerateContext(treeItem_cons, cons);
                this.Items.Add(treeItem_cons);
            }
        }

        /// <summary>
        /// Expand All Item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ExpandAll()
        {
            TreeItem_Root root = null;
            foreach (TreeViewItem item in this.Items)
            {
                if (item is TreeItem_Root)
                {
                    root = (TreeItem_Root)item;
                    break;
                }
            }
            if (root == null)
            {
                return;
            }
            subExpand(root);
        }
        /// <summary>
        /// 
        /// </summary>
        private void subExpand(TreeViewItem item)
        {
            item.IsExpanded = true;
            foreach (TreeViewItem i in item.Items)
            {
                subExpand(i);
            }
        }

        /// Generate Tree
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="item"></param>
        //protected override void generateTree(TreeObject obj, TreeViewItemControl item
        //    , TreeViewItemControl[] orgItems)
        //{
        //    if (obj is FeatureTreeNode
        //        || obj is FeatureTreeRoot)
        //    {
        //        INonLeaf nonLeaf = obj as INonLeaf;
        //        if (nonLeaf == null)
        //        {
        //            return;
        //        }
        //        foreach (INonRoot nonRoot in nonLeaf.Children)
        //        {
        //            TreeObject child = nonRoot as TreeObject;
        //            if (child == null)
        //            {
        //                continue;
        //            }

        //            TreeViewItemControl newItem = _menuGenerator.GenerateTreeItem(child, orgItems);
        //            newItem.Focusable = false;
        //            item.Items.Add(newItem);
        //            generateTree(child, newItem, orgItems);
        //        }
        //    }
        //    else if (obj is FeatureTreeGroupRelationship)
        //    {
        //        INonLeaf nonLeaf = obj as INonLeaf;
        //        if (nonLeaf == null)
        //        {
        //            return;
        //        }
        //        foreach (INonRoot nonRoot in nonLeaf.Children)
        //        {
        //            TreeObject child = nonRoot as TreeObject;
        //            if (child == null)
        //            {
        //                continue;
        //            }

        //            //TreeViewItemControl newItem = _menuGenerator.GenerateTreeItem(child, orgItems);
        //            //newItem.Focusable = false;
        //            //item.Items.Add(newItem);
        //            generateTree(child, item, orgItems);
        //        }
        //    }
        //}
    }
}
